<?php
namespace Home\Controller;

use Think\Controller;


class WeChatController extends Controller {
// appID
// wx5c6818578b37fb75
// appsecret
// 4b6c2c05625d657d2b1c13f15aa21372
	public function index()
    {
    	$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=APPID&secret=APPSECRET";
        echo 'access_token';

    }

//snsapi_base静默状态下获取信息snsapi_userinfo
    public function getBaseInfo(){
    	unset($_SEESION);
    	//1code
    	$appID = "wx5c6818578b37fb75";
    	$redirect_url = urlencode("http://2b7baf.natappfree.cc/thinkphp/index.php/home/WeChat/getUserOpenId");
    	$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appID."&redirect_uri=".$redirect_url."&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
    	// echo $url;die;
    	header('location:'.$url);
    	


    }
    public function http_curl($url){
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);  
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        $data = curl_exec($curl);  
        curl_close($curl);
        return $data;
    }

    public function getUserOpenId()
    {
    	//2access_token
    	$appID = "wx5c6818578b37fb75";
    	$appSecret = "4b6c2c05625d657d2b1c13f15aa21372";
    	$code = $_GET['code'];
    	$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appID."&secret=".$appSecret."&code=".$code."&grant_type=authorization_code";
    	// $res = $this->http_curl($url);
    	// dump($res);
    	// $url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
    	// $res = $this->http_curl($url);
    	// dump($res);



//3拉取用户的openid  （少量数据）
    	// $res = $this->_request($url);
    	// var_dump($res);


//直接获取（大量数据）
//定义公共函数
	// 得到access_token
    	$res = $this->sendRequest($url);
        // dump($res);
     // 根据access_token得到详细信息
        $access_token = $res["access_token"];
        $openId  = $res['openid'];
        $getUserInfo = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openId&lang=zh_CN";
        //得到用户信息
        $userdata = $this->sendRequest($getUserInfo);
        // dump($userdata);
        // die;
        $nickname = $userdata['nickname']; 
        $openid = $userdata['openid']; 
        $sex = $userdata['sex']; 
        $country = $userdata['country']; 
        $headimgurl = $userdata['headimgurl']; 
       
        $this->assign('openid',$openid);
        $this->assign('sex',getsex($sex));
        $this->assign('country',$country);
        $this->assign('headimgurl',$headimgurl);
        $this->assign('nickname',$nickname);


        // $user_info = 
        // $this->assign('data',$user_info);

        $this->display('index');

    }
//     array(9) {
//   ["openid"] => string(28) "o3Z2ks_mKrBSjf6FxA0jchmrA73g"
//   ["nickname"] => string(9) "天子笑"
//   ["sex"] => int(1)
//   ["language"] => string(5) "zh_CN"
//   ["city"] => string(0) ""
//   ["province"] => string(0) ""
//   ["country"] => string(6) "智利"
//   ["headimgurl"] => string(132) "http://thirdwx.qlogo.cn/mmopen/vi_32/8z8iaDJUlMWoQNkm5HXKzwbqlSfVw9ibg6l53NaicW1AG64W83MZ7yfaUyyibpnf5NDgUOzEsshiblSZGeQvHnFOaow/132"
//   ["privilege"] => array(0) {
//   }
// }

    

    //发送请求getUserOpenId 所需要调用到的方法
    public function sendRequest($url){

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);
        curl_close($ch);
        return json_decode($output, true);
    }
 

//设置网络请求配置getUserOpenId 所需要调用到的方法
public function _request($curl,$https=true,$method='GET',$data=null){
	// 创建一个新cURL资源
	$ch = curl_init();
	
	// 设置URL和相应的选项
	curl_setopt($ch, CURLOPT_URL, $curl);    //要访问的网站
	curl_setopt($ch, CURLOPT_HEADER, false);    //启用时会将头文件的信息作为数据流输出。
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);  //将curl_exec()获取的信息以字符串返回，而不是直接输出。 
 
	if($https){
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  //FALSE 禁止 cURL 验证对等证书（peer's certificate）。
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);  //验证主机
	}
	if($method == 'POST'){
		curl_setopt($ch, CURLOPT_POST, true);  //发送 POST 请求
		curl_setopt($ch, CURLOPT_POSTFIELDS, $data);  //全部数据使用HTTP协议中的 "POST" 操作来发送。
	}
	
	
	// 抓取URL并把它传递给浏览器
	$content = curl_exec($ch);
	if ($content  === false) {
	  return "网络请求出错: " . curl_error($ch);
	  exit();
	}
	//关闭cURL资源，并且释放系统资源
	curl_close($ch);
	
	return $content;
}

//snsapi_userinfo确认状态下获取信息
    public function getUserDetail()
    {
    	//1code
    	$appID = "wx5c6818578b37fb75";
    	$redirect_url = urlencode("http://2b7baf.natappfree.cc/thinkphp/index.php/home/WeChat/getUserInfo");
    	$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appID."&redirect_uri=".$redirect_url."&response_type=code&scope=snsapi_userinfo&state=123#wechat_redirect";
    	// echo $url;die;
    	header('location:'.$url);
    }

    public function getUserInfo()
    {
    	//2access_token
    	$appID = "wx5c6818578b37fb75";
    	$appSecret = "4b6c2c05625d657d2b1c13f15aa21372";
    	$code = $_GET['code'];
    	$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appID."&secret=".$appSecret."&code=".$code."&grant_type=authorization_code";
    	$re = $this->http_curl($url);
    	dump($re);die;
    	$access_token = $res['access_token'];
    	$openid = $res['openid'];

    	//3拉取用户详细信息
    	$url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
    	$res = $this->http_curl($url);
    	dump($res);
    }
}